-- UIPaymentWay
-- 选择充值方式界面
-- create by huanghr

require "game/ui/form/market/UIMyCardPay"

local PT_GOODS_ID = 10101;

UIPaymentWay = class("UIPaymentWay", function()
    return cc.Layer:create();
end);

function UIPaymentWay.create()
    return UIPaymentWay.new();
end

-- 构造函数
function UIPaymentWay:ctor()
    self:setName("UIPaymentWay");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/market/PaymentWay.csb");
    self:addChild(node);
    self.node = node;

    if DungeonM.isInDungeon() then
        -- 迷宫中层级要调高
        self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 11);
    end

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册各种点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIPaymentWay:registerTouchEvent()
    local node = self.node;

    -- MOL 支付
    local CT = findChildByName(node, "CT");
    local molBtn = findChildByName(CT, "mol_btn");
    local function onMolClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- TODO:
            MarketM.buyByRmb(PT_GOODS_ID, "mol");
            UIMgr.getCurrentScene():removeFormByName("UIPaymentWay");
        end
    end

    molBtn:addTouchEventListener(onMolClick);
    molBtn:setTitleText("");

    -- bluepay 支付
    local bluepayBtn = findChildByName(CT, "bluepay_btn");
    local function onBluepayClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- TODO:
            MarketM.buyByRmb(PT_GOODS_ID, "bluepay");
            UIMgr.getCurrentScene():removeFormByName("UIPaymentWay");
        end
    end

    bluepayBtn:addTouchEventListener(onBluepayClick);
    bluepayBtn:setTitleText("");

    -- mycard 支付
    local mycardBtn = findChildByName(CT, "mycard_btn");
    local function onMycardClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 打开mycard充值页面
            UIMgr.getCurrentScene():removeFormByName("UIMyCardPay");
            local uiForm = UIMyCardPay.create();
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 关闭本页面
            UIMgr.getCurrentScene():removeFormByName("UIPaymentWay");
        end
    end

    mycardBtn:addTouchEventListener(onMycardClick);
    mycardBtn:setTitleText("");

    -- 注册背景点击事件(点击背景，取消当前)
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIPaymentWay");
        end
    end
    bgImg:addTouchEventListener(onBgClick);
end

-- 注册事件处理回调函数
function UIPaymentWay:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIPaymentWay 界面析构清理");
            EventMgr.removeAll("UIPaymentWay");
        end
    end);
end

-- 重绘
function UIPaymentWay:redraw()
    local CT = findChildByName(self.node, "CT");

    -- 充值标题
    local titleLabel = findChildByName(CT, "title_label");

    -- TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_WHITE);
    titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
    titleLabel:setColor(TextStyleM.TEXT_COLOR_WHITE);

    local titleStr = getLocStr("apk_payment_method");
    titleLabel:setString(titleStr);

    -- 把配置的支付方式加入到可视列表中，未配置的支付方式需要隐藏
    local btnList = { "mycard_btn", "bluepay_btn", "mol_btn" };
    local visibleList = {};
    for _, payWay in ipairs(PAY_SDK) do
        local btnName = payWay .. "_btn";
        if table.indexOf(btnList, btnName) ~= -1 then
            table.insert(visibleList, btnName);
        end
    end

    local posY = 80;
    for _, btnName in ipairs(visibleList) do
        btn = findChildByName(CT, btnName);
        btn:setVisible(true);
        btn:setPosition(cc.p(0, posY));
        posY = posY - 100;
    end

    for _, btnName in ipairs(btnList) do
        if table.indexOf(visibleList, btnName) == -1 then
            btn = findChildByName(CT, btnName);
            btn:setVisible(false);
        end
    end
end

-- 适配
function UIPaymentWay:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end